package com.cskaoyan.service.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.bean.bo.user.AdminUserListBO;
import com.cskaoyan.bean.po.user.AdminUserAddressPO;
import com.cskaoyan.bean.po.user.AdminUserListPO;
import com.cskaoyan.bean.vo.user.AdminUserAddressVO;
import com.cskaoyan.mapper.user.AdminUserAddressMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/7/18 15:48
 */
@Service
@Data
public class AdminAddressServiceImpl implements AdminAddressService {
    private Integer limit;

    private Integer page;

    private Integer pages;

    private Integer total;

    private List<AdminUserAddressPO> list;
    @Autowired
    AdminUserAddressMapper adminUserAddressMapper;
    @Override
    public AdminUserAddressVO printAdminAddress(AdminUserListBO adminUserListBO) {
        this.page = adminUserListBO.getPage();
        limit = adminUserListBO.getLimit();
        Integer userId = adminUserListBO.getUserId();
        String name = adminUserListBO.getName();

        PageHelper.startPage(page, limit);
        if (userId == null && StringUtils.isEmpty(name)) {
            list = adminUserAddressMapper.selectList(null);

        } else {
            QueryWrapper<AdminUserAddressPO> adminUserListPOQueryWrapper = new QueryWrapper<>();

            adminUserListPOQueryWrapper.eq(!(userId == null), "id", userId);
            adminUserListPOQueryWrapper.like(!StringUtils.isEmpty(name), "name", name);
            list = adminUserAddressMapper.selectList(adminUserListPOQueryWrapper);
        }

        PageInfo<AdminUserAddressPO> adminUserAddressPOPageInfo = new PageInfo<>(list);
        total = (int) adminUserAddressPOPageInfo.getTotal();
        pages = adminUserAddressPOPageInfo.getPages();
        AdminUserAddressVO adminUserAddressVO = new AdminUserAddressVO();
        BeanUtil.copyBean(this, adminUserAddressVO);
        return adminUserAddressVO;
    }
}
